package com.hz.filemanager.model;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.hz.filemanager.R;
import com.hz.filemanager.model.file.ImageBean;
import com.hz.filemanager.model.file.ImgFolderBean;
import com.hz.filemanager.ui.MainActivity;
import com.hz.filemanager.ui.base.BaseActivity;
import com.hz.filemanager.ui.file.ImageActivity;
import com.hz.filemanager.utils.DensityUtil;
import com.hz.filemanager.utils.FileUtils;
import com.hz.filemanager.utils.Utils;
import com.kathline.library.content.ZFileBean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

public class DialogManager {
    private final static String TAG = "DialogManager";

    public static void renameDialog(Activity activity, String  filePath,String fileName, OnRenameListener listener){

        //拿到需要修改的路径
        String path = filePath;
        //以 . 分割路径后获得此文件后缀
        String[] split = fileName.split("\\.");
        final String suffix = split[split.length - 1];//获得后缀

        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_rename, null, false);
        Button okBtn = view.findViewById(R.id.ok_btn);
        Button cancelBtn = view.findViewById(R.id.cancel_btn);
        EditText renameEt = view.findViewById(R.id.rename_et);
        renameEt.setText(split[0]);

        final AlertDialog dialog = new AlertDialog.Builder(activity)
                .setView(view)
                .create();
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //修改时以 新文件名+.+文件类型后缀
                final String newFileName = renameEt.getText().toString().trim() + "." + suffix;
                if (FileUtils.rename(path, newFileName)) {
                    final String newPath = new File(path).getParent() + File.separator + newFileName;
                    ((BaseActivity)activity).notifyFileNameChanged(path, newPath);
                    if(listener != null){
                        listener.onSuccess(newFileName, newPath);
                    }
                } else {
                    if(listener != null){
                        listener.onFailed();
                    }
                }
                dialog.dismiss();
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setDialogWidthAndHeight(dialog, 320, 0);
    }

    public static void deleteFileDialog(Activity activity, OnDialogClickListener listener){

        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_delete_file, null, false);
        Button okBtn = view.findViewById(R.id.ok_btn);
        Button cancelBtn = view.findViewById(R.id.cancel_btn);

        final AlertDialog dialog = new AlertDialog.Builder(activity)
                .setView(view)
                .create();
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onOKClick(null);
                }
                dialog.dismiss();
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setDialogWidthAndHeight(dialog, 320, 0);

    }

    public static void rateDialog(Activity activity) {
        View contentView = activity.getLayoutInflater().inflate(R.layout.dialog_rate, null);
        final Dialog dialog = new Dialog(activity, R.style.Theme_DialogRate);
        dialog.setContentView(contentView);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        //window.setWindowAnimations(R.style.BottomDialog_Animation);
        Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(R.drawable.shape_dialog_rate);
        contentView.findViewById(R.id.rate_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public static void setDialogWidthAndHeight(Dialog dialog, int widthDp, int heightDp) {

        dialog.getWindow().setLayout(DensityUtil.dip2px(Utils.getContext(), widthDp), ConstraintLayout.LayoutParams.WRAP_CONTENT);

    }

    public static AlertDialog stopDialog(Activity activity, OnDialogClickListener listener) {
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_stop, null, false);
        TextView okBt = view.findViewById(R.id.stop_btn);
        TextView cancleBt = view.findViewById(R.id.cancel_btn);

        AlertDialog stopDialog = new AlertDialog.Builder(activity)
                .setView(view)
                .create();

        okBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopDialog.dismiss();
                if(listener != null){
                    listener.onOKClick(null);
                }
            }
        });
        cancleBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopDialog.dismiss();
            }
        });

        stopDialog.show();
        stopDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setDialogWidthAndHeight(stopDialog, 320,126);
        return stopDialog;
    }

    public interface OnDialogClickListener{
        void onOKClick(Object data);
    }

    public interface OnRenameListener{
        void onSuccess(String newName, String newPath);
        void onFailed();
    }
}
